package yuhuibear.poat.macro;

import java.util.HashSet;
import java.util.Set;

import yuhuibear.poat.annotations.action.Macro;
import yuhuibear.poat.exception.AnnotationConstrainException;

/**
 * This class used to convert Macro.class to MacroDefine with validation of :
 * <p>
 * 1. Macro name must include in definition statements.<Br>
 * 2. No upper case can be used in definition.
 * </p>
 * 
 * @author yuhuibear
 * 
 */
public class MacroDefine {

	public Class<?> targetClass;

	public String name;

	private Set<String> depends;

	public String sql;

	private String defPlace;

	public MacroDefine(Class<?> tgclass, String defPlace, String name,
			String depend, String sql) throws AnnotationConstrainException {
		targetClass = tgclass;
		if (!(sql.contains(name) || sql.toLowerCase().contains(name.toLowerCase())))
			throw new AnnotationConstrainException(targetClass, name, Macro.class, "Macro.name must be the name of the SQL object defined by the Macro ");
		for (char c : name.toCharArray())
			if (Character.isUpperCase(c))
				throw new AnnotationConstrainException(targetClass, name, Macro.class, "No upper case is allowed in Marco name.");
		this.name = name;
		this.sql = sql;
		this.defPlace = defPlace;
		depends = new HashSet<String>();
		if (depend.length() > 0) {
			String[] strs = depend.split(",\\s*");
			for (String n : strs) {
				n = n.trim();
				if (n.length() > 0)
					depends.add(n);
			}
		}

	}

	public Set<String> getDepends() {
		return depends;
	}

	public String getDefPlace() {
		return defPlace;
	}

	public String toString() {
		return String.format("name:%s, depend:%s, define at:%s, sq:%s", name, depends.toString(), defPlace, sql);
	}

}
